package com.tcdp.controllers.view;

import java.io.IOException;
import java.util.List;

import javax.persistence.NoResultException;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.tcdp.daos.BlogDao;
import com.tcdp.daos.BlogPostCommentDao;
import com.tcdp.delegates.BlogPostCommentDelegate;
import com.tcdp.delegates.BlogPostDelegate;
import com.tcdp.models.BlogPost;
import com.tcdp.models.BlogPostComment;
import com.tcdp.services.BlogPostCommentService;

@Controller
@EnableWebMvc
@RequestMapping("/blog")
public class ViewBlogPostController extends AbstractViewController {

	@RequestMapping(value = "/{category_url}/{blog_post_url}", method = RequestMethod.GET)
	public ModelAndView viewBlogPost(
			@PathVariable("category_url") String category_url,
			@PathVariable("blog_post_url") String blog_post_url,
			HttpServletResponse response) throws IOException {
		
		ModelAndView mv = new ModelAndView("BlogPost");

		BlogDao blogDao = new BlogDao();
		BlogPost blogPost = null;
		
		try{
			blogPost= blogDao.findByUrl(blog_post_url);
		}catch(NoResultException e){
			throwError(response, HttpServletResponse.SC_NOT_FOUND);
			return null;
		}
		
		BlogPostDelegate blogPostDel = new BlogPostDelegate(blogPost);

		BlogPostCommentDao commentDao = new BlogPostCommentDao();
		List<BlogPostComment> comments = commentDao.retrieveCommentByBlogPostId(blogPost.getBlogPostId());
		
		List<BlogPostCommentDelegate> commentsDel = BlogPostCommentService.convertToCommentDelegates(comments);
		
		mv.addObject("blogPost", blogPostDel);
		mv.addObject("comments", commentsDel);
		mv.addObject("newComment", new BlogPostCommentDelegate());

		return mv;
	}

	@RequestMapping(value = "/{category_url}/{blog_post_url}/addComment", method = RequestMethod.POST)
	public ModelAndView addCommentToBlog(
			@PathVariable("category_url") String category_url,
			@PathVariable("blog_post_url") String blog_post_url,
			@ModelAttribute("newComment") BlogPostCommentDelegate comment) {
		ModelAndView mv = new ModelAndView("redirect:/blog/"+category_url+"/"+blog_post_url);
		
		//no parent comment
		BlogPostCommentService.insertComment(comment, blog_post_url, -1);
		
		return mv;
	}
	
	@RequestMapping(value = "/{category_url}/{blog_post_url}/addComment/{parent_comment_id}", method = RequestMethod.POST)
	public ModelAndView addCommentToComment(
			@PathVariable("category_url") String category_url,
			@PathVariable("blog_post_url") String blog_post_url,
			@PathVariable("parent_comment_id") Integer parent_comment_id,
			@ModelAttribute("newChildComment") BlogPostCommentDelegate commentDel) {
		ModelAndView mv = new ModelAndView("redirect:/blog/"+category_url+"/"+blog_post_url);
		
		BlogPostCommentService.insertComment(commentDel, blog_post_url, parent_comment_id);
		
		return mv;
	}
	
	@RequestMapping(value = "/{category_url}/{blog_post_url}/generateForm/{parent_comment_id}", method = RequestMethod.POST)
	public ModelAndView generateForm(
			@PathVariable("category_url") String category_url,
			@PathVariable("blog_post_url") String blog_post_url,
			@PathVariable("parent_comment_id") String parent_comment_id) {
		ModelAndView mv = new ModelAndView("CommentForm");
		
		mv.addObject("blogCategoryUrl", category_url);
		mv.addObject("blogPostUrl", blog_post_url);
		mv.addObject("parentCommentId", parent_comment_id);
		mv.addObject("newChildComment", new BlogPostCommentDelegate());
		
		return mv;
	}
	
}
